<?php


namespace app\common\model\product;

use app\common\model\BaseModel;

/**
 * 商品标记模型
 */
class ProductMark extends BaseModel
{
    protected $pk = 'id';
    protected $name = 'product_mark';

    /**
     * 关联商品列表
     * @return \think\model\relation\HasMany
     * @Author joey
     * @date 2024/3/20 17:01
     */
    public function product()
    {
        return $this->hasMany(Product::class);
    }

    /**
     * 所有
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getALL($map = [])
    {
        $model = new static;
        if ($map) {
            $model = $model->where($map);
        }
        return $model->order(['sort' => 'asc', 'create_time' => 'asc'])->select();
    }

    /**
     * 详情
     */
    public static function detail($id)
    {
        return (new static())->find($id);
    }

    /**
     * 隐藏字段
     */
    public static function listData($where, $params, $with = [], $sort = 'sort asc,audit_status asc')
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $model = self::assWhere($model, $where);
        $list  = $model->paginate($params);
        return $list;
    }

    public static function createData($where)
    {
        $where           = self::disposeWhere($where);
        $model           = new static();
        $where['app_id'] = self::$app_id;
        $data            = $model->save($where);
        return $data;
    }

    public static function one($where)
    {
        $where = self::disposeWhere($where);
        $model = new static();
        return $model->findOrFail($where);
    }

    public static function deleteData($where)
    {
        $model = new static();
        return $model->where(['id' => $where['id']])->delete();
    }

    public static function listAll($where = [], $with = [], $sort = '')
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        $model = self::assWhere($model, $where);
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $list = $model->select();
        return $list;
    }

    /**
     * @Exp    获取数量
     * @Author Mr.Z
     * @Date   2024-04-26
     * @param  [type]     $param      [description]
     * @param  integer    $SupplierId [商户id]
     * @param  integer    $ProxyId    [代理id]
     * @return [type]                 [description]
     */
    public function getMarkTotal($param, $SupplierId=0, $ProxyId=0)
    {
        if($SupplierId){
            return $this->when($param, function ($query) use ($param) {
                $query->where($param);
            })
            ->where(['shop_supplier_id'=>$SupplierId])
            ->count();
        }
        if($ProxyId){
            return $this->when($param, function ($query) use ($param) {
                $query->where($param);
            })
            ->where(['proxy_id'=>$ProxyId])
            ->count();
        }
        return $this->when($param, function ($query) use ($param) {
            $query->where($param);
        })->count();
    }

    /**
     * 待审核数量
     * getAuditCount
     * int
     * @return int
     * @throws \think\db\exception\DbException
     * static
     * @throws \think\db\exception\DbException
     * @Author kevin
     * @date 2024/5/20 10:35
     */
    public static function getAuditCount(){
        $map = [];
        $map[] = ['audit_status','=',0];
        return self::where($map) -> count();

    }
}
